package net.zeus.sample.rabbitmq.utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ConnectionUtils {
	
	public static String SIMPLE_QUEUE_NAME = "test_simple_queue";
	
	public static String WORK_QUEUE_NAME = "test_work_queue";
	
	public static String WORK_FAIR_QUEUE_NAME = "test_work_fair_queue";
	
	//----------------
	public static String EXCHANGE_NAME = "test_exchange";
	
	public static String EXCHANGE_QUEUE_EMAIL = "test_exchange_queue_email";
	
	public static String EXCHANGE_QUEUE_SMS = "test_exchange_queue_SMS";
	
	//--------------
	public static String EXCHANGE_DIRECT_NAME = "test_exchange_direct";
	
	public static String EXCHANGE_DURECT_QUEUE_1 = "test_exchange_direct_queue_1";
	
	public static String EXCHANGE_DURECT_QUEUE_2 = "test_exchange_direct_queue_2";
	
	//-------------
	
	public static String EXCHANGE_TOPIC_NAME = "test_exchange_topic";
	
	public static String EXCHANGE_TOPIC_QUEUE_1 = "test_exchange_topic_queue_1";
	
	//-----------------
	public static String CONFIRM_QUEUE_NAME = "test_confirm_queue";
	
	public static Connection getConnection() {
		ConnectionFactory cf = new ConnectionFactory();
		cf.setHost("127.0.0.1");
		cf.setPort(5672);
		cf.setVirtualHost("/v_host1");
		cf.setUsername("michael");
		cf.setPassword("123");
		try {
			System.out.println("create connection success!");
			return cf.newConnection();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
		System.out.println("create connection f!");
		return null;
	}
	
	public static void closeConnection(Connection connection) {
		if(connection != null) {
			try {
				connection.close();
				System.out.println("close connection success!");
			} catch (IOException e) {
				System.out.println("close connection f!");
				e.printStackTrace();
			}
		}
	}
}
